/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
package org.cdinfusion.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.cdinfusion.data.CDElement;
import org.cdinfusion.data.GetInformation;
import org.cdinfusion.data.environment.Environment;
import org.cdinfusion.data.studytype.StudyType;
import org.cdinfusion.filehandling.CSVHandler;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class GSC_b_Controller extends AbstractController {

	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("##### GSC_b_Controller #####");

		
		final HttpSession session = request.getSession();
		final String origin = (String) session.getAttribute("origin");
		final String studytype = request.getParameter("StudyTypeDropDown");
		final String envtype = request.getParameter("EnvParamDropDown");
		final String sessionID = session.getId();
		final String submitbutton = request.getParameter("submit");
		
		final String radio = request.getParameter("radio");
		final List<CDElement> cdeLIST = new ArrayList<CDElement>();
		@SuppressWarnings("unchecked")
		final List<String> seqIDs = (List<String>) session
				.getAttribute("allSequenceIDs");
		
		String one_or_many = "";
		if (radio.equals("many cd sets for many")) {
			one_or_many = "many";
			session.setAttribute("one_or_many", one_or_many);
		} else {
			one_or_many = "one";
			session.setAttribute("one_or_many", one_or_many);
		}

		ModelAndView mav = null;

		if (radio.contains("one cd set for all")) {

			StudyType study = null;
			Environment env = null;
			
			try {
				study = (StudyType) Class.forName("org.cdinfusion.data.studytype." + studytype.toUpperCase()).newInstance();
			} catch (final Exception e) {
				System.out.println(e);
				System.out.printf("Could not create StudyType object for %s.", studytype);
			}
			try {
				env = (Environment)Class.forName("org.cdinfusion.data.environment." + StringUtils.capitalize(envtype)).newInstance();
			} catch (final Exception e) {
				System.out.println(e);
				System.out.printf("Could not create Environment object for %s.", envtype);
			}
			
			if (env == null || study == null || "none".equals(studytype) || "none".equals(envtype)) {
				ModelAndView errmav = new ModelAndView("ErrorPage");
				errmav.addObject("origin", origin);
				return errmav;
			}
			

			final int numberOfSequences = ((Integer) session
					.getAttribute("numberOfSequencesUploaded")).intValue();
			CDElement cdelement = new CDElement(seqIDs.get(0),
					seqIDs.get(seqIDs.size() - 1), numberOfSequences, study,
					env, null, false);
			cdeLIST.add(cdelement);

			session.setAttribute("cdeLIST", cdeLIST);

			// TODO: Exception handling

			session.setAttribute("opt_2b", "one");
			session.setAttribute("study", studytype);
			session.setAttribute("env", envtype);

			if (envtype.equals("none") & studytype.equals("none")) {
				ModelAndView errmav = new ModelAndView("ErrorPage");
				errmav.addObject("origin", origin);
				return errmav;
			}// end none if

			if (submitbutton.contains("download empty csv")) {
				CSVHandler csvh = new CSVHandler();
				csvh.generateEmptyCSV(studytype, envtype, sessionID,
						seqIDs);
				mav = new ModelAndView("CSV_CREATION");
				mav.addObject("origin", origin);

				return (mav);
			}// end button if

			final String[][] envAttributes = GetInformation.nameInformation(env);
			final String[][] studyAttributes = GetInformation.nameInformation(study);

			mav = new ModelAndView("3b_CD_INPUT_1tom");
			mav.addObject("origin", origin);
			mav.addObject("envAttributes", envAttributes);
			mav.addObject("studyAttributes", studyAttributes);
			mav.addObject("studyName", GetInformation.getLongNameForStudy(study));
			mav.addObject("study", studytype);
			mav.addObject("environment", envtype);
			mav.addObject("environmentName", GetInformation.getLongNameForEnvironment(env));

			return mav;
		} else if (radio.contains("many cd sets for many")) {
			session.setAttribute("opt_2b", "many");
			mav = new ModelAndView("3b1_CD_INPUT_ntom");
			mav.addObject("origin", origin);

			return mav;
		}// end if (radio.contains("many cd sets for many") == true)
		
		return mav;
	}
}